<html>
<head><meta charset="utf-8"><title>Exhaustiveness · project-deref-patterns · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/index.html">project-deref-patterns</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html">Exhaustiveness</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244991893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/244991893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Cameron <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#244991893">(Jul 06 2021 at 03:17)</a>:</h4>
<p>Another idea for handling the exhaustiveness issue, when we use a deref pattern, generate an implicit wildcard branch which panics (unless there is an explicit one). E.g.,</p>
<div class="codehilite"><pre><span></span><code>fn proposed(a: Option&lt;Rc&lt;bool&gt;&gt;) {
    match a.as_ref() {
        Some(&amp;true) =&gt; {}
        Some(&amp;false) =&gt; {}
        // This is considered reachable code by the compiler, and if it were absent, would be implicitly generated.
        Some(&amp;_) =&gt; unreachable!(),
        None =&gt; {}
    }
}
</code></pre></div>



<a name="244991966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/244991966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Cameron <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#244991966">(Jul 06 2021 at 03:19)</a>:</h4>
<p>AIUI, this solution is ergonomic (no _ boilerplate) and sound (no ub, worst case scenario in the case of non-idempotent deref is that we panic, no ub).</p>



<a name="244992065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/244992065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Cameron <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#244992065">(Jul 06 2021 at 03:21)</a>:</h4>
<p>I.e., deref should be idempotent, but if it is not, then we panic. I think this is an acceptable level of safety for operator overloading, somewhat like indexing having an implicit panic on out of bounds access (though completely different too :-) )</p>



<a name="245019424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/245019424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#245019424">(Jul 06 2021 at 10:22)</a>:</h4>
<p>Perhaps this could work for user-defined derefs in the future, though, for stdlib types it doesn't need to happen. That may only be an argument for an optimization, though,</p>



<a name="245107169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/245107169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Cameron <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#245107169">(Jul 06 2021 at 22:37)</a>:</h4>
<p>I think it would be nice to have a rule that should work in the general case so that we don't need to rely on the limitation to std for correctness, only to limit the blast radius of the feature. Otherwise, we are not getting the experience we would need in order to expand the feature later.</p>



<a name="245107264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/281601-project-deref-patterns/topic/Exhaustiveness/near/245107264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Cameron <a href="https://rust-lang.github.io/zulip_archive/stream/281601-project-deref-patterns/topic/Exhaustiveness.html#245107264">(Jul 06 2021 at 22:38)</a>:</h4>
<p>Also, the less we need to rely on a hardcoded list the better, IMO, seems like that is just technical debt waiting to happen</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>